package main.java.tmi.popup.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.util.AjaxPageSplit;
import main.java.tmi.customer.service.CustomerService;
import main.java.tmi.order.service.OrderService;
import main.java.tmi.popup.service.PopupService;
import main.java.tmi.returncause.service.ReturncauseService;
import main.java.tmi.returncause.service.ReturncauseVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class PopupController {
	
    @Resource(name = "popupServiceImpl")
    private PopupService popupService;
    
    @Resource(name = "returncauseServiceImpl")
    private ReturncauseService returncauseService;
    
    /**
     * The Constructor
     */
    public PopupController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/customerPop")
    public String viewCustomerPop(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model, String customer, String messrsCode) throws SQLException {
        model.addAttribute("customer",customer);
        model.addAttribute("messrsCode",messrsCode);
        return "/tmi/popup/customerPop";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/customerPop2")
    public String viewCustomerPop2(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model, String customer, String messrsCode) throws SQLException {
        model.addAttribute("custCode",param.get("custCode"));
        model.addAttribute("customer",customer);
        model.addAttribute("shipperCode",messrsCode);
        return "/tmi/popup/customerPop2";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productPop")
    public String viewProductPop(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        model.addAttribute("goodsCode",param.get("goodsCode"));
        model.addAttribute("custCode",param.get("custCode"));
        return "/tmi/popup/productPop";
    }

   /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/stockOutPop")
    public String viewStockOutPop(HttpServletRequest request, HttpServletResponse response,
            ModelMap model, String rowId) throws SQLException {
        model.addAttribute("rowId",rowId);
        return "/tmi/popup/stockOutPop";
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/stockOutNoPop")
    public String viewStockOutNoPop(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {

        return "/tmi/popup/stockOutNoPop";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getCustomerPopView")
    public void getCustomerPopView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int customeCount = popupService.getCustomerPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(customeCount, pagingTmp, listScale, 5, "");
		System.out.println("===========pageSplit============> "+pageSplit);
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("customeCount",customeCount);
		model.addAttribute("resultList",popupService.getCustomerPopView(param));
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getProductPopView")
    public void getProductPopView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int productCount = popupService.getProductPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(productCount, pagingTmp, listScale, 5, "");
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("productCount",productCount);
		model.addAttribute("resultList",popupService.getProductPopView(param));
    }
    
    /**
     * StockOut search
     */
    @RequestMapping("/getStockOutPopUpView")
    public void getStockOutPopView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int stockOutCount = popupService.getStockOutPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(stockOutCount, pagingTmp, listScale, 5, "");

		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("stockOutCount",stockOutCount);
		model.addAttribute("resultList",popupService.getStockOutPopView(param));
    }   
    
    /**
     * StockOut search
     */
    @RequestMapping("/getStockOutNoPopView")
    public void getStockOutNoPopView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int stockOutNoCount = popupService.getStockOutNoPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(stockOutNoCount, pagingTmp, listScale, 5, "");
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("stockOutNoCount",stockOutNoCount);
		model.addAttribute("resultList",popupService.getStockOutNoPopView(param));
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/orderNumPop")
    public String orderNumPop(HttpServletRequest request, HttpServletResponse response,
            @RequestParam Map param, ModelMap model) throws SQLException {

        return "/tmi/popup/orderNumPop";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/orderNumPopView")
    public void orderNumPopView(@RequestParam Map param, Model model) throws Exception {
        
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
        int listScale = Integer.parseInt(param.get("listScale").toString());
        int paging = (pagingTmp-1)*listScale;
        
        param.put("paging", paging);
        
        int orderNumCount = popupService.getOrderNumListCount(param);
        
        String pageSplit = pageSplitMgr.getSplitPageLink(orderNumCount , pagingTmp, listScale, 5, "");

        model.addAttribute("pageSplit",pageSplit);
        model.addAttribute("orderNumCount",orderNumCount );
        model.addAttribute("resultList",popupService.getOrderNumList(param));
    }
    
    
    

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/packingNumPop")
    public String pacingNumPop(HttpServletRequest request, HttpServletResponse response,
            @RequestParam Map param, ModelMap model) throws SQLException {

        return "/tmi/popup/packingNumPop";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/packingNumPopView")
    public void packingNumPopView(@RequestParam Map param, Model model) throws Exception {
        
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
        int listScale = Integer.parseInt(param.get("listScale").toString());
        int paging = (pagingTmp-1)*listScale;
        
        param.put("paging", paging);
        
        int packingNumCount = popupService.getPackingNumListCount(param);
        
        String pageSplit = pageSplitMgr.getSplitPageLink(packingNumCount , pagingTmp, listScale, 5, "");

        model.addAttribute("pageSplit",pageSplit);
        model.addAttribute("packingNumCount",packingNumCount );
        model.addAttribute("resultList",popupService.getPackingNumList(param));
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/returnProductPop")
    public String viewReturnProductPop(HttpServletRequest request, HttpServletResponse response,
            ModelMap model, String rowId) throws SQLException {

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("paging", "0");
		paramMap.put("listScale", "100000");
		List<ReturncauseVO> listReturnCause = returncauseService.getReturncauseView(paramMap);
        model.addAttribute("listReturnCause", listReturnCause);
        model.addAttribute("rowId",rowId);
        return "/tmi/popup/returnProductPop";
    }

    /**
     * StockOut search
     */
    @RequestMapping("/getReturnProductPopUpView")
    public void getReturnProductPopUpView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int returnProducPopCount = popupService.getReturnProducPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(returnProducPopCount, pagingTmp, listScale, 5, "");
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("returnProducPopCount",returnProducPopCount);
		model.addAttribute("resultList",popupService.getReturnProducPopView(param));
    } 
    
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/mawbHawbNumPop")
    public String mawbHawbNumPop(HttpServletRequest request, HttpServletResponse response,
            @RequestParam Map param, ModelMap model, String menuType) throws SQLException {
        model.addAttribute("menuType",menuType);
        return "/tmi/popup/mawbHawbNumPop";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/mawbHawbNumPopView")
    public void mawbHawbNumPopView(@RequestParam Map param, Model model) throws Exception {
        
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
        int listScale = Integer.parseInt(param.get("listScale").toString());
        int paging = (pagingTmp-1)*listScale;
        
        param.put("paging", paging);
        
        int mawbHawbNumCount = popupService.getMawbHawbNumListCount(param);
        
        String pageSplit = pageSplitMgr.getSplitPageLink(mawbHawbNumCount , pagingTmp, listScale, 5, "");

        model.addAttribute("pageSplit",pageSplit);
        model.addAttribute("mawbHawbNumCount",mawbHawbNumCount );
        model.addAttribute("resultList",popupService.getMawbHawbNumList(param));
    }
    
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/returnNoPop")
    public String viewrReturnNoPop(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {

        return "/tmi/popup/returnNoPop";
    }
    
    /**
     * StockOut search
     */
    @RequestMapping("/getReturnNoPopView")
    public void getReturnNoPopView(@RequestParam Map param, Model model) throws Exception {
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int returnNoCount = popupService.getReturnNoPopCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(returnNoCount, pagingTmp, listScale, 5, "");
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("returnNoCount",returnNoCount);
		model.addAttribute("resultList",popupService.getReturnNoPopView(param));
    }
    
}
